<?php
/**

 * stone pei added
 * webform render logic
 */
function webformview_render_DE(&$form, &$form_state) {
  //---------- validte country ----------------
  if (isset($_REQUEST['country'])) {
    $this_nid = $form['#node']->nid;
    $return = webform_get_nid_by_key($_REQUEST['country']);
    if ( $return['nid'] != $this_nid ) {
      drupal_goto('node/' . $this_nid);
    }
  }
  $country = (isset($_REQUEST['country']) && $_REQUEST['country']) ? $_REQUEST['country'] : 'DE';
  $country = strtoupper($country);
  
  if( isset( $form['#submission']->data[COUNTRY_OF_DOMESTIC_CID]['value'] ) ){
    $country_of_domestic = $form['#submission']->data[COUNTRY_OF_DOMESTIC_CID]['value'][0];
    $country = strtoupper($country_of_domestic);
  };
  //get country from file upload
  if(isset($_POST['submitted']['country_of_domestic_transfer'])){
    $country = check_plain($_POST['submitted']['country_of_domestic_transfer']);
  }
  $transfer_country = 'country_of_domestic_transfer';
//  print_r($form['submitted'][$transfer_country]['#options']);
//  exit;
      //Set default value
  unset($form['submitted'][$transfer_country]['#empty_value']);
  $form['submitted'][$transfer_country]['#default_value'] = $country;
  
  if (isset($form['submitted'][$transfer_country]['#options'])) {
    foreach ($form['submitted'][$transfer_country]['#options'] as $key => $val) {
      if ($key != $country) {
        unset($form['submitted'][$transfer_country]['#options'][$key]);
      }
    }
  }
  //---------- end validate ----------------
}

function webformview_render_JAP(&$form, &$form_state){
  //---------- validte country ----------------
  if (isset($_REQUEST['country'])) {
    $this_nid = $form['#node']->nid;
    $return = webform_get_nid_by_key($_REQUEST['country']);
    if ( $return['nid'] != $this_nid ) {
      drupal_goto('node/' . $this_nid);
    }
  }
  //---------- end validate ----------------
}
function webformview_render_MEX(&$form, &$form_state){
  //---------- validte country ----------------
  if (isset($_REQUEST['country'])) {
    $this_nid = $form['#node']->nid;
    $return = webform_get_nid_by_key($_REQUEST['country']);
    if ( $return['nid'] != $this_nid ) {
      drupal_goto('node/' . $this_nid);
    }
  }
  //---------- end validate ----------------
}
function webformview_render_TUR(&$form, &$form_state){
  //---------- validte country ----------------
  if (isset($_REQUEST['country'])) {
    $this_nid = $form['#node']->nid;
    $return = webform_get_nid_by_key($_REQUEST['country']);
    if ( $return['nid'] != $this_nid ) {
      drupal_goto('node/' . $this_nid);
    }
  }
  //---------- end validate ----------------
}

function webformview_render_AU(&$form, &$form_state){
  
  $country = (isset($_REQUEST['country']) && $_REQUEST['country']) ? $_REQUEST['country'] : 'AU';
  $country = strtoupper($country);
  
  //---------- validte country ----------------
  if (isset($_REQUEST['country'])) {
    $this_nid = $form['#node']->nid;
    $return = webform_get_nid_by_key($_REQUEST['country']);
    if ( $return['nid'] != $this_nid ) {
      drupal_goto('node/' . $this_nid);
    }
  }
  //---------- end validate ----------------
  //Edit webform page
  if( isset( $form['#submission']->data[COUNTRY_OF_DOMESTIC_CID]['value'] ) ){
    $country_of_domestic = $form['#submission']->data[COUNTRY_OF_DOMESTIC_CID]['value'][0];
    $country = strtoupper($country_of_domestic);
  };
  //get country from file upload
  if(isset($_POST['submitted']['country_of_domestic_transfer'])){
    $country = check_plain($_POST['submitted']['country_of_domestic_transfer']);
  }
  
  $transfer_country = 'country_of_domestic_transfer';
  $relocation_data = 'relocation_data';
  $relocation_type = 'relocation_type';

  // Display component field
  if (isset($form['submitted'][$transfer_country]['#options'])) {
    foreach ($form['submitted'][$transfer_country]['#options'] as $key => $val) {
      if ($key != $country) {
        unset($form['submitted'][$transfer_country]['#options'][$key]);
      }
    }
  }
  //Set default value
  unset($form['submitted'][$transfer_country]['#empty_value']);
  $form['submitted'][$transfer_country]['#default_value'] = $country;


  // Define component field key
  $relocation_type_arr['AU'] = array(
      'au_temporary_domestic' => '',
      'au_permanent_domestic' => '',
      'au_return_to_home_domestic' => '',
  );
  $relocation_type_arr['FR'] = array(
      'fr_permanent' => '',
  );
  $relocation_type_arr['IN'] = array(
      'in_company_initiated_transfer' => '',
      'in_new_joinee_relocation' => '',
      'in_relocating_home_country_base' =>'',
  );
  $relocation_type_arr['IT'] = array(
      'it_temporary_transfer' => '',
      'it_permanent_transfer' => '',
  );
  $relocation_type_arr['MY'] = array(
      'my_permanent' => '',
      'my_temporary' => '',
      'my_return_to_home_location' => '',
  );
  $relocation_type_arr['PL'] = array(
      'pl_temporary' => '',
      'pl_permanent' => '',
      'pl_temporary_repatriation' => '',
      'pl_temporary_retirement' => '',
      'pl_temporary_company_separation' => '',
      'pl_temporary_resignation' => '',
      'pl_temporary_personal_request_repatriation' => '',
  );
  $relocation_type_arr['CH'] = array(
      'ch_temporary' => '',
      'ch_permanent' => '',
      'ch_temporary_return_home' => '',
  );
  $relocation_type_arr['ES'] = array(
      'es_temporary' => '',
      'es_permanent' => '',
      'es_temporary_return_home' => '',
      'es_temporary_company_separation' => '',
  );
  $relocation_type_arr['PK'] = array(
      'pk_new_hire_permanent_transfer' => '',
      'pk_new_hire_return_home' => '',
      'pk_experienced_employee_permanent' => '',
      'pk_experienced_employee_temporary' => '',
      'pk_experienced_employee_return_home_repatriation' => '',
      'pk_experienced_employee_return_home_resignation' => '',
      'pk_experienced_employee_return_home_retirement' => '',
      'pk_experienced_employee_return_home_company_initiated_separation' => '',
      'pk_experienced_employee_personal_request' => '',
      '' => '',
  );
  $relocation_type_arr['RU'] = array(
      'ru_permanent' => '',
      'ru_temporary' => '',
      'ru_personal_request' => '',
      'ru_return_home_repatriation' => '',
      'ru_return_home_retirement' => '',
      'ru_return_home_company_separation' => '',
      'ru_return_home_resignation' => '',
      'ru_new_hire' => '',
      'ru_temporary_to_permanent' => '',
      'ru_return_home' => '',    
  );
    $relocation_type_arr['RO'] = array(
        'ro_experienced_temporary' => '',
        'ro_experienced_permanent' => '',
        'ro_experienced_return_home' => '',
        'ro_new_hire_temporary' => '',
        'ro_new_hire_permanent' => '',
        'ro_extended_business_traveller_(ebt)' => '',
    );
    $relocation_type_arr['PH'] = array(
        'ph_local_employee' => '',
        'ph_localized_employee' => '',
        'ph_personal_request' => '',
        'ph_return_to_home_domestic' => '',
    );
    $relocation_type_arr['CN'] = array(
        'cn_local-to-local_permanent_relocation' => '',
        'cn_temporary_relocation' => '',
        'cn_local_extended_business_travel' => '',
        'cn_fpe_domestic_relocation' => '',
        'cn_returning_to_home_company_home_base' => '',
        'cn_personal_request_temporary_transfer' => '',
        'cn_termination_while_on_temporary_transfer' => '',
        'cn_plant_technology_transfer_and_training' => '',
        'cn_termination_while_on_temporary_transfer_employee' => '',
    );
    $relocation_type_arr['ID'] = array(
        'id_standard_locations' => '',
        'id_special_locations' => '',
    );
    $relocation_type_arr['CO'] = array(
        'co_temporary_transfers_' => '',
        'co_local_transfers' => '',
        'co_transfer_back_home' => '',
    );
    $relocation_type_arr['VE'] = array(
        've_permanent_transfers' => '',
        've_temporary_transfers' => '',
        've_transfer_back_home' => '',
    );
    $relocation_type_arr['HR'] = array(
        'hr_permanent' => '',
        'hr_temporary' => '',
        'hr_temporary_return_home_repatriation' => '',
        'hr_temporary_return_home_resignation' => '',
        'hr_temporary_return_home_retirement' => '',
        'hr_temporary_return_home_company_initiated_separation' => '',
    );
    $relocation_type_arr['CZ'] = array(
        'cz_permanent' => '',
        'cz_temporary' => '',
        'cz_temporary_return_home_repatriation' => '',
        'cz_temporary_return_home_resignation' => '',
        'cz_temporary_return_home_retirement' => '',
        'cz_temporary_return_home_company_initiated_separation' => '',
    );
    $relocation_type_arr['HU'] = array(
        'hu_permanent' => '',
        'hu_temporary' => '',
        'hu_temporary_return_home_repatriation' => '',
        'hu_temporary_return_home_resignation' => '',
        'hu_temporary_return_home_retirement' => '',
        'hu_temporary_return_home_company_initiated_separation' => '',
    );
    $relocation_type_arr['SK'] = array(
        'sk_permanent' => '',
        'sk_temporary' => '',
        'sk_temporary_return_home_repatriation' => '',
        'sk_temporary_return_home_resignation' => '',
        'sk_temporary_return_home_retirement' => '',
        'sk_temporary_return_home_company_initiated_separation' => '',
    );
    $relocation_type_arr['AR'] = array(
        'ar_permanent' => '',
        'ar_temporary' => '',
    );
    $relocation_type_arr['CL'] = array(
        'cl_permanent' => '',
    );
    $relocation_type_arr['GR'] = array(
        'gr_permanent_transfer' => '',
        'gr_temporary_transfer' => '',
        'gr_return_home' => '',
    );
    $relocation_type_arr['PT'] = array(
        'pt_temporary_transfers' => '',
        'pt_permanent_transfers' => '',
        'pt_return_home' => '',
    );
    $relocation_type_arr['MA'] = array(
        'ma_permanent_transfers' => '',
        'ma_temporary_transfers' => '',
        'ma_return_home_from_temporary_transfers' => '',
        'ma_temporary_to_permanent_transfer' => '',
    );
    $relocation_type_arr['NL'] = array(
        'nl_permanent_transfers' => '',
        'nl_temporary_transfers' => '',
        'nl_return_home_from_temporary_transfers' => '',
    );
  $search_array = $relocation_type_arr[$country];
  if (isset($form['submitted'][$relocation_data][$relocation_type]['#options'])) {
    foreach ($form['submitted'][$relocation_data][$relocation_type]['#options'] as $key => $val) {
      if (!array_key_exists($key, $search_array)) {
        unset($form['submitted'][$relocation_data][$relocation_type]['#options'][$key]);
      }
    }
  }

}